//
//  oldWatingViewController.m
//  laonianbang
//
//  Created by 勇拓 李 on 18/3/13.
//  Copyright © 2018年 zaiyou. All rights reserved.
//

#import "oldWatingViewController.h"
#import "oldGifView.h"
#import "oldMessageManager.h"
#import "oldPublishDemandViewController.h"

@interface oldWatingViewController ()<oldMessageDelegate>
@property (weak, nonatomic) IBOutlet UILabel *label_number;
@property (weak, nonatomic) IBOutlet UIButton *btn_cancel;
@property (weak, nonatomic) IBOutlet UIView *baseView;
@property (nonatomic, strong)NSTimer *timer;
@property (nonatomic, assign)NSInteger time;
@property (nonatomic, strong)oldMessageManager *manager;
@end

@implementation oldWatingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self initData];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [oldGifView endLoading:self.baseView];
    [self closeTimer];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    UIViewController *vc = self.navigationController.viewControllers[[self.navigationController.viewControllers count] - 1];
    
    if ([vc isKindOfClass:[oldPublishDemandViewController class]]) {
        oldPublishDemandViewController *oldVc = (oldPublishDemandViewController *)vc;
        oldVc.ifSearching = NO;
        oldVc.number = _number;
        oldVc.count = _count;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - initView
- (void)initView
{
    self.navigationItem.title = @"寻找中";
    self.btn_cancel.layer.cornerRadius = 6.f;
    [_btn_cancel addTarget:self action:@selector(btn_cancel_tapped:) forControlEvents:UIControlEventTouchDown];
    
    [oldGifView startLoading:self.baseView];
    [self initMessage];
}

- (void)btn_cancel_tapped:(id)sender
{
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"是否取消当前订单?" message:@"可能有工作人员已经看到了您的订单" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *chooseReason = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self cancel];
    }];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    
    [alert addAction:chooseReason];
    [alert addAction:cancel];
    
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)closeTimer
{
    if (_timer) {
        if ([_timer isValid]) {
            [_timer invalidate];
        }
        _timer = nil;
    }
}

#pragma mark - initData
- (void)initData
{
    [self.timer invalidate];
    self.timer = nil;
    
    self.timer = [NSTimer timerWithTimeInterval:.5 target:self selector:@selector(timerBegin) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    //将计时器放入runloop中
    [self timerBegin];
}

- (void)timerBegin
{
    if (_number < 20) {
        _number++;
        _count++;
    }
    else if (_number < 40)
    {
        _count ++;
        _number = 20 + (_count - 20)/2;
    }
    else if (_number < 100)
    {
        _count ++;
        _number = 35 + (_count - 40)/4;
    }
    
    _label_number.text = [NSString stringWithFormat:@"%ld", (long)_number];
}

#pragma mark - message
- (void)initMessage
{
    _manager = [[oldMessageManager alloc] initWithType:orderAll];
    _manager.delegate = self;
    [_manager registerForNotifications];
}

- (void)receiveMessage:(id)message type:(oldMessageType)type
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - cancel
- (void)cancel
{
    [oldHub waiting:@"正在取消中.."];
    
    WeakSelf
    
    [OLDNetWork httpRequestWithPostPut:URL_CancelPublish params:nil success:^(id data) {
        
        UIViewController *vc = weakSelf.navigationController.viewControllers[[weakSelf.navigationController.viewControllers count] - 2];
        
        if ([vc isKindOfClass:[oldPublishDemandViewController class]]) {
            oldPublishDemandViewController *oldVc = (oldPublishDemandViewController *)vc;
            oldVc.ifSearching = NO;
        }
        [weakSelf.navigationController popViewControllerAnimated:YES];
        [oldHub warningUserAfterJump:@"订单已取消" time:1.0];
    } error:^(NSString *error) {
        [oldHub warningUser:error];
    }];
}

@end
